const WXAPI = require('../../wxapi/main')
const app = getApp()
Page({
  data: {
    goodsList: {
      totalPrice: 0,
      allSelect: false,
      list: []
    },
    delBtnWidth: 120, //删除按钮宽度单位（rpx）
  },

  //获取元素自适应后的实际宽度
  getEleWidth: function(w) {
    var real = 0;
    try {
      var res = wx.getSystemInfoSync().windowWidth;
      var scale = (750 / 2) / (w / 2); //以宽度750px设计稿做宽度的自适应
      // console.log(scale);
      real = Math.floor(res / scale);
      return real;
    } catch (e) {
      return false;
      // Do something when catch error
    }
  },
  initEleWidth: function() {
    var delBtnWidth = this.getEleWidth(this.data.delBtnWidth);
    this.setData({
      delBtnWidth: delBtnWidth
    });
  },
  onLoad: function() {
    this.initEleWidth();
    this.onShow();
  },
  onShow: function() { 
    var that = this;
    // 获取购物车数据
    WXAPI.carlist().then(function (res) {
      var shopList = [];
      if (res.type == 'success') {
        shopList = res.resultData.rows;
      }
      that.data.goodsList.list = shopList;
      that.setGoodsList();
    })   
  },
  toIndexPage: function() {
    wx.switchTab({
      url: "/pages/index/index"
    });
  },

  touchS: function(e) {
    if (e.touches.length == 1) {
      this.setData({
        startX: e.touches[0].clientX
      });
    }
  },
  touchM: function(e) {
    var index = e.currentTarget.dataset.index;

    if (e.touches.length == 1) {
      var moveX = e.touches[0].clientX;
      var disX = this.data.startX - moveX;
      var delBtnWidth = this.data.delBtnWidth;
      var left = "";
      if (disX == 0 || disX < 0) { //如果移动距离小于等于0，container位置不变
        left = "margin-left:0px";
      } else if (disX > 0) { //移动距离大于0，container left值等于手指移动距离
        left = "margin-left:-" + disX + "px";
        if (disX >= delBtnWidth) {
          left = "left:-" + delBtnWidth + "px";
        }
      }
      var list = this.data.goodsList.list;
      if (index != "" && index != null) {
        list[parseInt(index)].left = left;
        this.setGoodsList();
      }
    }
  },

  touchE: function(e) {
    var index = e.currentTarget.dataset.index;
    if (e.changedTouches.length == 1) {
      var endX = e.changedTouches[0].clientX;
      var disX = this.data.startX - endX;
      var delBtnWidth = this.data.delBtnWidth;
      //如果距离小于删除按钮的1/2，不显示删除按钮
      var left = disX > delBtnWidth / 2 ? "margin-left:-" + delBtnWidth + "px" : "margin-left:0px";
      var list = this.data.goodsList.list;
      if (index !== "" && index != null) {
        list[parseInt(index)].left = left;
        this.setGoodsList();

      }
    }
  },
  delItem: function(e) {
    var index = e.currentTarget.dataset.index;
    var list = this.data.goodsList.list;
    var id = list[index].id;
    WXAPI.carDel(id).then(function (res) {

    })  
    list.splice(index, 1);
    this.setGoodsList();
     
  },
  selectTap: function(e) {
      var id = e.currentTarget.dataset.id;
      var car = this.getone(id);
      car.active = !car.active
      this.setGoodsList();
  },
  getone:function(id){
    //获取一个节点
    var list = this.data.goodsList.list;
    if (id !== "" && id != null) {
      for (var i = 0; i < list.length; i++) {
        var curItem = list[i];
        for (var m = 0; m < curItem.carlist.length; m++) {
          var car = curItem.carlist[m];
          if (car.id == id) {
            return car;
          }
        }
      }
    }
  },
  totalPrice: function() {
    var list = this.data.goodsList.list;
    var total = 0;
    for (var i = 0; i < list.length; i++) {
      var curItem = list[i];
      for (var m = 0; m < curItem.carlist.length; m++) {
        var car = curItem.carlist[m];
        if(car.active){
          total += parseFloat(car.baobei.salePrice) * car.bbnum;
        } 
      }
    }
    total = parseFloat(total.toFixed(2)); //js浮点计算bug，取两位小数精度
    return total;
  },
  
  setGoodsList: function() {
    var goodlist = this.data.goodsList;
    var list = goodlist.list;
    goodlist.totalPrice = this.totalPrice();
    this.setData({
      goodsList: goodlist
    });
    var shopCarInfo = {};
    var tempNumber = 0;
    shopCarInfo.shopList = list;
    for (var i = 0; i < list.length; i++) {
      tempNumber = tempNumber + list[i].number
    }
    shopCarInfo.shopNum = tempNumber;
    wx.setStorage({
      key: "shopCarInfo",
      data: shopCarInfo
    })
  },
  bindAllSelect: function() {
    var currentAllSelect = !this.data.goodsList.allSelect;
    this.data.goodsList.allSelect = currentAllSelect;
    var list = this.data.goodsList.list;
    for (var i = 0; i < list.length; i++) {
      var curItem = list[i];
      for (var m = 0; m < curItem.carlist.length; m++) {
        var car = curItem.carlist[m];
        car.active = currentAllSelect;
      }
    }
    this.setData({
      goodsList: this.data.goodsList
    });
    this.setGoodsList();
  },
  jiaBtnTap: function(e) {
    var that = this
    var index = e.currentTarget.dataset.index;
    var car = that.getone(index)
    car.bbnum++;
    that.setGoodsList();
  },
  jianBtnTap: function(e) {
    var that = this
    var index = e.currentTarget.dataset.index;
    var car = that.getone(index);
    if (car.bbnum > 1){
      car.bbnum--;
      that.setGoodsList();
    }  
  },
  deleteSelected: function() {
    var list = this.data.goodsList.list;
    list = list.filter(function(curGoods) {
      if (curGoods.active){
        var id = curGoods.id;
        WXAPI.carDel(id).then(function (res) {
        })  
      }
      return !curGoods.active;
    });
    var list = this.data.goodsList.list;
    for (var i = 0; i < list.length; i++) {
      var curItem = list[i];
      curItem.carlist = curItem.carlist.filter(function (curGoods) {
        if (curGoods.active) {
          var id = curGoods.id;
          WXAPI.carDel(id).then(function (res) {
          })
        }
        return !curGoods.active;
      });
    }
    this.setGoodsList();
  },
  toPayOrder: function() {
    wx.showLoading();
    var that = this;

    var list = this.data.goodsList.list;
    var shopList = [];
    for (var i = 0; i < list.length; i++) {
      var curItem = list[i];
      for (var m = 0; m < curItem.carlist.length; m++) {
        var car = curItem.carlist[m];
        if (car.active){
          shopList.push(car);
        }
      }
      if (shopList.length>0){
        break;
      }
    }

    if (shopList.length == 0) {
      wx.hideLoading();
      return;
    }
    // 写入本地存储
    wx.setStorage({
      key: "carInfo",
      data: shopList
    })
    that.navigateToPayOrder();

  },
  navigateToPayOrder: function() {
    wx.hideLoading();
    wx.navigateTo({
      url: "/pages/to-pay-order/index"
    })
  }



})